<template>
  <div class="goods_list">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>添加商品</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <div class="left">
        <el-input
          v-model="list.query"
          class="ipt"
          clearable
          placeholder="请输入内容"
        >
        </el-input>
        <el-button class="el-icon-search" type="info" @click="getGoodsList"></el-button>
        <el-button @click="tiaozhuan">添加商品</el-button>
      </div>
      <el-table
        :data="tableData"
        border
        stripe
        style="width: 100%">
        <el-table-column
          type="index">
        </el-table-column>
        <el-table-column
          label="商品名称"
          prop="goods_name"
          width="700">
        </el-table-column>
        <el-table-column
          label="商品价格"
          prop="goods_price"
          width="180">
        </el-table-column>
        <el-table-column
          label="商品重量"
          prop="goods_weight">
        </el-table-column>
        <el-table-column
          label="创建时间"
          prop="add_time">
        </el-table-column>
        <el-table-column
          label="操作">
          <template #default="{row}">
            <el-button>编辑</el-button>
            <el-button @click="delGoods(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :current-page.sync="list.pagenum"
        :page-size="list.pagesize"
        :page-sizes="[10,20,30]"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import { delGoods, getGoodslist } from '@/api/goods'

export default {
  name: 'hm-goods',
  data () {
    return {
      user: {},
      tableData: [],
      total: 0,
      list: {
        query: '',
        pagenum: 1,
        pagesize: 10
      }
    }
  },
  created () {
    this.getGoodsList()
  },
  methods: {
    // 删除商品
    async delGoods (row) {
      const res = await delGoods(row.goods_id)
      this.$message(res.meta.msg)
      this.getGoodsList()
    },
    async getGoodsList () {
      const res = await getGoodslist(this.list)
      this.tableData = res.data.goods
      this.total = res.data.total
    },
    // 监听page size 方框
    handleSizeChange (newsize) {
      console.log(newsize)
      this.list.pagesize = newsize
      this.getGoodsList()
    },
    // 监听current change 页码值
    handleCurrentChange (newcurrent) {
      this.list.pagenum = newcurrent
      this.getGoodsList()
    },
    tiaozhuan () {
      this.$router.push('add')
    }
  }

}
</script>

<style lang="scss" scoped>
.goods_list {
  .left {
    display: flex;
    justify-content: left;
    align-items: center;
    height: 40px;
    margin-bottom: 20px;

    .ipt {
      width: 410px;
    }

    .el-button {
      height: 40px;
    }
  }
}
</style>
